package ConnectionsCommands;

import Gear.Factory;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implement received command to remove a specified file.
 *
 * @author Rodrigo Martins Racanicci
 */
public class ConnectionCommandRemoveFile implements ConnectionCommand {

    @Override
    public void execute(Socket client, String[] args) {
        try {
            Factory.getInstance().getTracker().getFileMutex().acquire();

            if ( (Factory.getInstance().getTracker().isLocked(args[1])) || (!Factory.getInstance().getTracker().verifyFile(client.getInetAddress().toString(), args[1]))) {
                Factory.getInstance().getTracker().sendCommand(client, "false");
            } else {
                Factory.getInstance().getTracker().removeFile(client.getInetAddress().toString(), args[1]);
                Factory.getInstance().getTracker().sendCommand(client, "true");
            }
            Factory.getInstance().getTracker().closeClient(client);
            Factory.getInstance().getTracker().getFileMutex().release();
        } catch (InterruptedException ex) {
            Logger.getLogger(ConnectionCommandRemoveFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
